package cn.iocoder.yudao.module.knowledge.controller.admin.elecdataln;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.knowledge.controller.admin.elecdataln.vo.*;
import cn.iocoder.yudao.module.knowledge.dal.dataobject.elecdataln.ElecDataLnDO;
import cn.iocoder.yudao.module.knowledge.service.elecdataln.ElecDataLnService;

@Tag(name = "管理后台 - 用电信息查询")
@RestController
@RequestMapping("/knowledge/elec-data-ln")
@Validated
public class ElecDataLnController {

    @Resource
    private ElecDataLnService elecDataLnService;

    @PostMapping("/create")
    @Operation(summary = "创建用电信息查询")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-ln:create')")
    public CommonResult<Long> createElecDataLn(@Valid @RequestBody ElecDataLnSaveReqVO createReqVO) {
        return success(elecDataLnService.createElecDataLn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用电信息查询")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-ln:update')")
    public CommonResult<Boolean> updateElecDataLn(@Valid @RequestBody ElecDataLnSaveReqVO updateReqVO) {
        elecDataLnService.updateElecDataLn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用电信息查询")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-ln:delete')")
    public CommonResult<Boolean> deleteElecDataLn(@RequestParam("id") Long id) {
        elecDataLnService.deleteElecDataLn(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用电信息查询")
                @PreAuthorize("@ss.hasPermission('knowledge:elec-data-ln:delete')")
    public CommonResult<Boolean> deleteElecDataLnList(@RequestParam("ids") List<Long> ids) {
        elecDataLnService.deleteElecDataLnListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用电信息查询")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-ln:query')")
    public CommonResult<ElecDataLnRespVO> getElecDataLn(@RequestParam("id") Long id) {
        ElecDataLnDO elecDataLn = elecDataLnService.getElecDataLn(id);
        return success(BeanUtils.toBean(elecDataLn, ElecDataLnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用电信息查询分页")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-ln:query')")
    public CommonResult<PageResult<ElecDataLnRespVO>> getElecDataLnPage(@Valid ElecDataLnPageReqVO pageReqVO) {
        PageResult<ElecDataLnDO> pageResult = elecDataLnService.getElecDataLnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ElecDataLnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用电信息查询 Excel")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-ln:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportElecDataLnExcel(@Valid ElecDataLnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ElecDataLnDO> list = elecDataLnService.getElecDataLnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用电信息查询.xls", "数据", ElecDataLnRespVO.class,
                        BeanUtils.toBean(list, ElecDataLnRespVO.class));
    }

}